/**
 * 获取用户信息，不需要参数，手机端会自动根据登录的账号获取用户信息
 * @param obj
 */
let apiGetUserInfor = function(obj) {
    let that = this // 初始化整个jsAPI的this对象
    that.cjcLog('apiGetUserInfor start first')
    // 定义返回的数据
    let resultData = {
        msg: '',
        status: ''
    }
    // 回调执行Promise函数
    return new Promise((resolve, reject) => {
        try {
            // app session 是否有操作权限
            that.cjcAuth({
                success: function(AuthData) { // 方法执行成功回调 AuthData为安全验证返回的值
                    that.cjcLog('auth success')
                    // 统计 或 埋点
                    that.cjcAnalys('apiGetUserInfor')
                    that.cjcLog('api/apiGetUserInfor start')
                    // 发起请求
                    window.WebViewJavascriptBridge.callHandler('getUserInfor', null, function(response) {
                        // 方法执行成功
                        that.cjcLog('api/GetUserInfor end')
                        // 确认至json对象
                        that.cjcLog('response:' + that.printObject(response))
                        if (typeof response === 'string') {
                            response = JSON.parse(response)
                        }
                        // 返回状态值为0成功
                        if (response.status === 0) {
                            that.cjcLog('apiGetUserInfor:' + that.printObject(response))
                            // Promise状态更改为resolve
                            resolve(response)
                        } else {
                            // 设置返回数据状态值
                            resultData.msg = response.msg
                            resultData.status = response.status
                            that.cjcLog('apiGetUserInfor:' + that.printObject(resultData))
                            reject(resultData)
                        }
                    })
                },
                fail: function(data) { // 方法执行失败回调
                    that.cjcLog('auth fail')
                    that.cjcLog('apiGetUserInfor:' + that.printObject(data))
                }
            })
        } catch (e) {
            // 方法执行失败
            // 方法执行失败
            resultData.msg = '登录失败 异常'
            resultData.status = '4'
            that.cjcLog('apiGetUserInfor:' + that.printObject(resultData))
        }
    })
}
/**
 * 创建群聊
 * @param obj
 */
let createTribe = function(obj) {
    let that = this // 初始化整个jsAPI的this对象
    that.cjcLog('createTribe start first')
    // 定义返回的数据
    let resultData = {
        msg: '',
        status: ''
    }
    // 回调执行Promise函数
    return new Promise((resolve, reject) => {
        try {
            // app session 是否有操作权限
            that.cjcAuth({
                success: function(AuthData) { // 方法执行成功回调 AuthData为安全验证返回的值
                    that.cjcLog('auth success')
                    // 统计 或 埋点
                    that.cjcAnalys('createTribe')
                    that.cjcLog('api/createTribe start')
                    // 发起请求
                    window.WebViewJavascriptBridge.callHandler('createTribe', obj, function(response) {
                        // 方法执行成功
                        that.cjcLog('api/createTribe end')
                        // 确认至json对象
                        that.cjcLog('response:' + that.printObject(response))
                        if (typeof response === 'string') {
                            response = JSON.parse(response)
                        }
                        // 返回状态值为0成功
                        if (response.status === 0) {
                            that.cjcLog('createTribe:' + that.printObject(response))
                            // Promise状态更改为resolve
                            resolve(response)
                        } else {
                            // 设置返回数据状态值
                            resultData.msg = response.msg
                            resultData.status = response.status
                            that.cjcLog('createTribe:' + that.printObject(resultData))
                            reject(resultData)
                        }
                    })
                },
                fail: function(data) { // 方法执行失败回调
                    that.cjcLog('auth fail')
                    that.cjcLog('createGroup:' + that.printObject(data))
                }
            })
        } catch (e) {
            // 方法执行失败
            // 方法执行失败
            resultData.msg = '登录失败 异常'
            resultData.status = '4'
            that.cjcLog('createGroup:' + that.printObject(resultData))
        }
    })
}
/**
 * 加入群
 * @param obj
 */
let joinToTribe = function(obj) {
    let that = this // 初始化整个jsAPI的this对象
    that.cjcLog('joinToTribe start first')
    // 定义返回的数据
    let resultData = {
        msg: '',
        status: ''
    }
    // 回调执行Promise函数
    return new Promise((resolve, reject) => {
        try {
            // app session 是否有操作权限
            that.cjcAuth({
                success: function(AuthData) { // 方法执行成功回调 AuthData为安全验证返回的值
                    that.cjcLog('auth success')
                    // 统计 或 埋点
                    that.cjcAnalys('joinToTribe')
                    that.cjcLog('api/joinToTribe start')
                    // 发起请求
                    window.WebViewJavascriptBridge.callHandler('joinToTribe', obj, function(response) {
                        // 方法执行成功
                        that.cjcLog('api/joinToTribe end')
                        // 确认至json对象
                        that.cjcLog('response:' + that.printObject(response))
                        if (typeof response === 'string') {
                            response = JSON.parse(response)
                        }
                        // 返回状态值为0成功
                        if (response.status === 0) {
                            that.cjcLog('joinToTribe:' + that.printObject(response))
                            // Promise状态更改为resolve
                            resolve(response)
                        } else {
                            // 设置返回数据状态值
                            resultData.msg = response.msg
                            resultData.status = response.status
                            that.cjcLog('joinToTribe:' + that.printObject(resultData))
                            reject(resultData)
                        }
                    })
                },
                fail: function(data) { // 方法执行失败回调
                    that.cjcLog('auth fail')
                    that.cjcLog('joinToTribe:' + that.printObject(data))
                }
            })
        } catch (e) {
            // 方法执行失败
            // 方法执行失败
            resultData.msg = '登录失败 异常'
            resultData.status = '4'
            that.cjcLog('joinToTribe:' + that.printObject(resultData))
        }
    })
}
/**
 * 打开群聊
 * @param obj tribeId 群ID
 */
let openTribe = function(obj) {
    let that = this // 初始化整个jsAPI的this对象
    that.cjcLog('openTribe start first')
    // 定义返回的数据
    let resultData = {
        msg: '',
        status: ''
    }
    // 回调执行Promise函数
    return new Promise((resolve, reject) => {
        try {
            // app session 是否有操作权限
            that.cjcAuth({
                success: function(AuthData) { // 方法执行成功回调 AuthData为安全验证返回的值
                    that.cjcLog('auth success')
                    // 统计 或 埋点
                    that.cjcAnalys('openTribe')
                    that.cjcLog('api/openTribe start')
                    // 发起请求
                    window.WebViewJavascriptBridge.callHandler('openTribe', obj, function(response) {
                        // 方法执行成功
                        that.cjcLog('api/openTribe end')
                        // 确认至json对象
                        that.cjcLog('response:' + that.printObject(response))
                        if (typeof response === 'string') {
                            response = JSON.parse(response)
                        }
                        // 返回状态值为0成功
                        if (response.status === 0) {
                            that.cjcLog('openTribe:' + that.printObject(response))
                            // Promise状态更改为resolve
                            resolve(response)
                        } else {
                            // 设置返回数据状态值
                            resultData.msg = response.msg
                            resultData.status = response.status
                            that.cjcLog('openTribe:' + that.printObject(resultData))
                            reject(resultData)
                        }
                    })
                },
                fail: function(data) { // 方法执行失败回调
                    that.cjcLog('auth fail')
                    that.cjcLog('openTribe:' + that.printObject(data))
                }
            })
        } catch (e) {
            // 方法执行失败
            // 方法执行失败
            resultData.msg = '登录失败 异常'
            resultData.status = '4'
            that.cjcLog('openTribe:' + that.printObject(resultData))
        }
    })
}
/**
 * 刷新常用微服务列表
 * @param obj noticeName
 */
let noticeBroad = function(obj) {
    let that = this // 初始化整个jsAPI的this对象
    that.cjcLog('noticeBroad start first')
    // 定义返回的数据
    let resultData = {
        msg: '',
        status: ''
    }
    // 回调执行Promise函数
    return new Promise((resolve, reject) => {
        try {
            // app session 是否有操作权限
            that.cjcAuth({
                success: function(AuthData) { // 方法执行成功回调 AuthData为安全验证返回的值
                    that.cjcLog('auth success')
                    // 统计 或 埋点
                    that.cjcAnalys('noticeBroad')
                    that.cjcLog('api/noticeBroad start')
                    // 发起请求
                    window.WebViewJavascriptBridge.callHandler('noticeBroad', obj, function(response) {
                        // 方法执行成功
                        that.cjcLog('api/noticeBroad end')
                        // 确认至json对象
                        that.cjcLog('response:' + that.printObject(response))
                        if (typeof response === 'string') {
                            response = JSON.parse(response)
                        }
                        // 返回状态值为0成功
                        if (response.status === 0) {
                            that.cjcLog('noticeBroad:' + that.printObject(response))
                            // Promise状态更改为resolve
                            resolve(response)
                        } else {
                            // 设置返回数据状态值
                            resultData.msg = response.msg
                            resultData.status = response.status
                            that.cjcLog('noticeBroad:' + that.printObject(resultData))
                            reject(resultData)
                        }
                    })
                },
                fail: function(data) { // 方法执行失败回调
                    that.cjcLog('auth fail')
                    that.cjcLog('noticeBroad:' + that.printObject(data))
                }
            })
        } catch (e) {
            // 方法执行失败
            // 方法执行失败
            resultData.msg = '登录失败 异常'
            resultData.status = '4'
            that.cjcLog('noticeBroad:' + that.printObject(resultData))
        }
    })
}
/**
 * 打开单聊
 * @param obj openPerson
 */
let openPerson = function(obj) {
    let that = this // 初始化整个jsAPI的this对象
    that.cjcLog('openPerson start first')
    // 定义返回的数据
    let resultData = {
        msg: '',
        status: ''
    }
    // 回调执行Promise函数
    return new Promise((resolve, reject) => {
        try {
            // app session 是否有操作权限
            that.cjcAuth({
                success: function(AuthData) { // 方法执行成功回调 AuthData为安全验证返回的值
                    that.cjcLog('auth success')
                    // 统计 或 埋点
                    that.cjcAnalys('openPerson')
                    that.cjcLog('api/openPerson start')
                    // 发起请求
                    window.WebViewJavascriptBridge.callHandler('openPerson', obj, function(response) {
                        // 方法执行成功
                        that.cjcLog('api/openPerson end')
                        // 确认至json对象
                        that.cjcLog('response:' + that.printObject(response))
                        if (typeof response === 'string') {
                            response = JSON.parse(response)
                        }
                        // 返回状态值为0成功
                        if (response.status === 0) {
                            that.cjcLog('openPerson:' + that.printObject(response))
                            // Promise状态更改为resolve
                            resolve(response)
                        } else {
                            // 设置返回数据状态值
                            resultData.msg = response.msg
                            resultData.status = response.status
                            that.cjcLog('openPerson:' + that.printObject(resultData))
                            reject(resultData)
                        }
                    })
                },
                fail: function(data) { // 方法执行失败回调
                    that.cjcLog('auth fail')
                    that.cjcLog('openPerson:' + that.printObject(data))
                }
            })
        } catch (e) {
            // 方法执行失败
            // 方法执行失败
            resultData.msg = '登录失败 异常'
            resultData.status = '4'
            that.cjcLog('openPerson:' + that.printObject(resultData))
        }
    })
}
/**
 * 打开对应key的person单聊
 * @param obj openPersonWithKey
 */
let openPersonWithKey = function(obj) {
    let that = this // 初始化整个jsAPI的this对象
    that.cjcLog('openPersonWithKey start first')
    // 定义返回的数据
    let resultData = {
        msg: '',
        status: ''
    }
    // 回调执行Promise函数
    return new Promise((resolve, reject) => {
        try {
            // app session 是否有操作权限
            that.cjcAuth({
                success: function(AuthData) { // 方法执行成功回调 AuthData为安全验证返回的值
                    that.cjcLog('auth success')
                    // 统计 或 埋点
                    that.cjcAnalys('openPersonWithKey')
                    that.cjcLog('api/openPersonWithKey start')
                    // 发起请求
                    window.WebViewJavascriptBridge.callHandler('openPersonWithKey', obj, function(response) {
                        // 方法执行成功
                        that.cjcLog('api/openPersonWithKey end')
                        // 确认至json对象
                        that.cjcLog('response:' + that.printObject(response))
                        if (typeof response === 'string') {
                            response = JSON.parse(response)
                        }
                        // 返回状态值为0成功
                        if (response.status === 0) {
                            that.cjcLog('openPersonWithKey:' + that.printObject(response))
                            // Promise状态更改为resolve
                            resolve(response)
                        } else {
                            // 设置返回数据状态值
                            resultData.msg = response.msg
                            resultData.status = response.status
                            that.cjcLog('openPersonWithKey:' + that.printObject(resultData))
                            reject(resultData)
                        }
                    })
                },
                fail: function(data) { // 方法执行失败回调
                    that.cjcLog('auth fail')
                    that.cjcLog('openPersonWithKey:' + that.printObject(data))
                }
            })
        } catch (e) {
            // 方法执行失败
            // 方法执行失败
            resultData.msg = '登录失败 异常'
            resultData.status = '4'
            that.cjcLog('openPersonWithKey:' + that.printObject(resultData))
        }
    })
}

let shakeOneShake = function() {
    let that = this
    that.cjcLog('调用摇一摇的方法')
    return new Promise((resolve, reject) => {
        window.WebViewJavascriptBridge.registerHandler('shakeOneShake', function(data, responseCallback) {
            var responseData = {
                status: 0
            }
            responseCallback(responseData)
            resolve(data)
        })
    })
}

/***
 * 加载本页API
 * @param obj
 */
let loadUserApi = function(obj) {
    obj.apiGetUserInfor = apiGetUserInfor
    obj.createTribe = createTribe
    obj.joinToTribe = joinToTribe
    obj.openTribe = openTribe
    obj.noticeBroad = noticeBroad
    obj.openPerson = openPerson
    obj.openPersonWithKey = openPersonWithKey
    obj.shakeOneShake = shakeOneShake
}
export {
    loadUserApi
}
